import os
import subprocess
import struct

def is_elf_file(file_path):
    try:
        with open(file_path, 'rb') as f:
            magic_number = f.read(4)
        return magic_number == b'\x7fELF'
    except (IOError, OSError):
        return False

def find_elf_files(directory):
    elf_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            if is_elf_file(file_path):
                elf_files.append(file_path)
    return elf_files

def get_exported_symbols(file_path):
    try:
        output = subprocess.check_output(["readelf", "-s", file_path], universal_newlines=True)
        return output.splitlines()
    except subprocess.CalledProcessError:
        return []

def parse_exported_symbols(lines):
    exported_symbols = []
    for line in lines:
        parts = line.split()
        if len(parts) >= 8 and parts[0].isdigit():
            symbol_name = parts[-1]
            is_undefined = parts[3] == "UND"
            so_name = parts[-2]
            exported_symbols.append((symbol_name, is_undefined, so_name))
    return exported_symbols

def print_symbols(elf_files):
    for elf_file in elf_files:
        print(f"Symbols for: {elf_file}")
        symbols = get_exported_symbols(elf_file)
        parsed_symbols = parse_exported_symbols(symbols)
        for symbol_name, is_undefined, so_name in parsed_symbols:
            print(f"{symbol_name}  {is_undefined}  {so_name}")
        print("")

if __name__ == "__main__":
    current_directory = os.getcwd()
    elf_files = find_elf_files(current_directory)
    if not elf_files:
        print("No ELF files found in the current directory and its subdirectories.")
    else:
        print_symbols(elf_files)
